<template>
  <view class="container" v-if="m_isLoaded">
    <view class="js">
      <view class="i">
        <view class="l">订单编号：</view>
        <view class="r">{{ m_MallPayment.FBillNo }}</view>
      </view>
      <view class="i">
        <view class="l">支付金额：</view>
        <view class="r">{{
          parseFloat(m_MallPayment.FPayRealAmount).toFixed(2)
        }}</view>
      </view>
      <view class="i">
        <view class="l;">支付方式：</view>
        <view class="r">{{ m_MallPayment.PayName }}</view>
      </view>
    </view>
    <view class="b-c">
      <view class="btn" v-on:tap="onImmediatePay">立即支付</view>
    </view>
  </view>
</template>

<script>
import { ref } from "@vue/composition-api";
import { reqPost } from "../../common/http.request";
import {
  uGetStorageSync,
  uRedirectTo,
  uSwitchTab,
  uToast,
} from "../../utils/uniUtils";

export default {
  async onLoad({ id }) {
    if (id) {
      try {
        this.id = id;
        await this.GetPageData(id);
        this.m_isLoaded = true;
      } catch (error) {
        this.m_isLoaded = false;
        uToast("初始化失败！", "error");
        setTimeout(() => {
          uSwitchTab("/pages/tab_bar/index");
        }, 1500);
      }
    } else {
      uToast("订单不存在！", "error");
      setTimeout(() => {
        uSwitchTab("/pages/tab_bar/index");
      }, 1500);
    }
  },
  setup(props) {
    const m_MallPayment = ref({});
    const m_isLoaded = ref(false);
    const id = ref(0);
    async function GetPageData(id) {
      m_MallPayment.value = (
        await reqPost("GetMallPayment", {
          KInterID: id,
        })
      ).Data[0];
    }

    //支付
    async function onImmediatePay() {
      const res = await reqPost("SetRenewalsPay", {
        openid: uGetStorageSync("userInfo").openId,
        total_fee: parseFloat(m_MallPayment.value.FPayRealAmount).toFixed(2),
        years: 0,
        detaileds: JSON.stringify([
          {
            checked: true,
            FItemID: m_MallPayment.value.KInterID,
            number: m_MallPayment.value.FBillNo, // 本地订单号
          },
        ]),
      });
      const paySign = res.Data?.respData?.paySign;
      if (paySign) {
        await uni.requestPayment({
          provider: "wxpay",
          timeStamp: res.Data?.respData?.payTimeStamp,
          nonceStr: res.Data?.respData?.paynonceStr,
          package: res.Data?.respData?.payPackage,
          signType: res.Data?.respData?.paySignType,
          paySign: paySign,
          success(res) {
            console.log("res :>> ", res);
            uni.redirectTo({
              url:
                "/pages/pay/pay_success/index?orderId=" +
                m_MallPayment.value.FBillNo,
            });
          },
          fail(err) {
            uToast("支付失败~", "error");
            setTimeout(() => {
              uRedirectTo("/pages/order/OrderList?l=0");
            }, 1500);
          },
        });
      }
    }

    return { m_isLoaded, GetPageData, m_MallPayment, onImmediatePay, id };
  },
};
</script>

<style lang="scss" scoped>
@import "./scss/OrderToPay.scss";
</style>
